package com.goudan.mybatisplus.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.goudan.mybatisplus.dao.LgdUserDao;
import com.goudan.mybatisplus.entity.LgdUser;
import com.goudan.mybatisplus.service.LgdUserService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/demo")
public class DemoController {

    @Resource
    private LgdUserService lgdUserService;

    @Resource
    private LgdUserDao lgdUserDao;

    /**
     * http://127.0.0.1:8080/demo/selectUserPage
     */
    @RequestMapping("selectUserPage")
    public List<LgdUser> selectUserPage() {
        Page<LgdUser> page = new Page<>(1, 10);
        QueryWrapper<LgdUser> wrapper = Wrappers.query();
        wrapper.eq("USER_NAME", "金彪666");
        Page<LgdUser> userPage = lgdUserService.page(page, wrapper);
        return userPage.getRecords();
    }

    /**
     * http://127.0.0.1:8080/demo/selectUserLambda?age=21
     */
    @RequestMapping("selectUserLambda")
    public List<LgdUser> selectUserLambda(String age) {
        QueryWrapper<LgdUser> wrapper = Wrappers.query();
        wrapper.lambda().eq(LgdUser::getUserName, "金彪")
                .ge(!StringUtils.isEmpty(age), LgdUser::getAge, age)
                .likeRight(LgdUser::getAddress, "66")
                .orderByAsc(LgdUser::getId);
        return lgdUserService.list(wrapper);
    }

    @RequestMapping("updateById")
    public boolean updateById(Long id) {
        LgdUser user = new LgdUser();
        user.setId(id);
        user.setUserName("苟蛋");
        return lgdUserService.updateById(user);
    }

    @RequestMapping("updateAllColumnById")
    public Integer updateAllColumnById(Long id) {
        LgdUser user = new LgdUser();
        user.setId(id);
        user.setUserName("苟蛋");
        return lgdUserDao.alwaysUpdateSomeColumnById(user);
    }


}
